//
//  SquareCommitTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/2/12.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "SquareCommitTableViewCell.h"


@interface SquareCommitTableViewCell ()

// 背景
@property (nonatomic, weak) UIView * backView;
// 头像
@property (nonatomic, weak) UIImageView * headImageView;
// 名称
@property (nonatomic, weak) UILabel * nameLab;
// 内容
@property (nonatomic, weak) UILabel * contentLabel;
// 时间
@property (nonatomic, weak) UILabel * timeLabel;
// 性别
@property (nonatomic, weak) UIImageView * genderImageView;
//下级评论背景
//@property (nonatomic, weak) UITableView * tableView;
@property (nonatomic, weak)UIButton * moreButton;

@property (nonatomic, strong) NSArray <ChildModel *> * listArray;

@property (nonatomic, weak)UIView  * commiteBackView;
@property (nonatomic, weak)UILabel * commitName;
@property (nonatomic, weak)UILabel * commitContentLabel;
@property (nonatomic, strong)NSMutableArray * nameIDArray;
@property (nonatomic, strong)NSMutableArray * nameArray;
@property (nonatomic, strong)NSMutableArray * repleyIDArray;

@property (nonatomic, copy)NSString * ID;
@property (nonatomic, copy)NSString * user_name;

@end

@implementation SquareCommitTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.nameIDArray = [NSMutableArray array];
        self.nameArray = [NSMutableArray array];
        self.repleyIDArray = [NSMutableArray array];
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UIView * backView = [[UIView alloc] init];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    // 头像视图
    UIImageView * headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, kBlank, kFaceWidth, kFaceWidth)];
    headImageView.contentMode = UIViewContentModeScaleAspectFill;
    //    headImageView.userInteractionEnabled = YES;
    headImageView.layer.cornerRadius = 20 * SCREENH6;
    headImageView.layer.masksToBounds = YES;
    
    self.headImageView = headImageView;
    
    [self.backView addSubview:self.headImageView];
    // 名字视图
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.headImageView.right + 10 * SCREENH6, self.headImageView.top, kTextWidth, 15 * SCREENH6)];
    nameLabel.font = [UIFont systemFontOfSize:13.0 * SCREENH6];
    nameLabel.textColor = [UIColor lightGrayColor];
    nameLabel.backgroundColor = ZRWhiteColor;
    
    self.nameLab = nameLabel;
    
    [self.backView addSubview:self.nameLab];
    
    // 性别视图
    UIImageView * genderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.nameLab.left, self.nameLab.bottom + 3 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6)];
    genderImageView.contentMode = UIViewContentModeScaleAspectFill;
    //    headImageView.userInteractionEnabled = YES;
    
    self.genderImageView = genderImageView;
    
    [self.backView addSubview:self.genderImageView];
    
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.left, self.genderImageView.bottom + 3 * SCREENH6, 150 * SCREENH6, 13 * SCREENH6)];
    
    timeLabel.textColor = ColorWithRGB(0x909090);
    timeLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * contentLabel = [[UILabel alloc] init];
    
    contentLabel.textColor = ZRBlackColor;
    contentLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    contentLabel.numberOfLines = 0;
    contentLabel.userInteractionEnabled = YES;
     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [contentLabel addGestureRecognizer:tapGR];
    
    self.contentLabel = contentLabel;
    
    [self.backView addSubview:self.contentLabel];
    
    
//    UITableView * tableView = [[UITableView alloc] init];
//    tableView.delegate = self;
//    tableView.dataSource = self;
//    tableView.backgroundColor = ColorWithRGB(0xF5F5F5);
////    tableView.rowHeight = 60 * SCREENH6;
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//
//    self.tableView = tableView;
//
//    [self.tableView registerClass:[SecondaryCommitTableViewCell class] forCellReuseIdentifier:@"SecondaryCommitTableViewCell"];
//
//    [self.backView addSubview:self.tableView];
    
    
    UIView * commitBackView = [UIView new];
    
    commitBackView.backgroundColor = ColorWithRGB(0xF5F5F5);
    commitBackView.userInteractionEnabled = YES;
    
    self.commiteBackView = commitBackView;
    
    [self.backView addSubview:self.commiteBackView];
}

- (void)setModel:(SquareCommitModel *)model{
    
    _model = model;
    
    self.ID = [NSString stringWithFormat:@"%@",model.id];
    self.user_name = model.user_name;
    
//    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.user_headimg];
//    NSURL * url = [NSURL URLWithString:iconStr];
//    // 头像
//    [self.headImageView sd_setImageWithURL:url];
    if([model.user_headimg containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:model.user_headimg];
        [self.headImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.user_headimg];
        NSURL * url = [NSURL URLWithString:iconStr];
        [self.headImageView sd_setImageWithURL:url];
    }
    // 昵称
    self.nameLab.text = model.user_name;
    int gender = [model.user_sex intValue];
    if (gender == 1) {
        
        self.genderImageView.image = [UIImage imageNamed:@"男性"];
    }else if (gender == 2){
        
        self.genderImageView.image = [UIImage imageNamed:@"女性"];
    }
    
    self.timeLabel.text = model.inputtime;
    self.contentLabel.text = model.content;
    
    CGFloat bottom = self.timeLabel.bottom + kPaddingValue;
//    CGFloat rowHeight = 0;
    
    CGSize attrStrSize = [self preferredSizeWithMaxWidth:275 * SCREENH6 withLabel:self.contentLabel];
    CGFloat labH = attrStrSize.height;
    
    self.contentLabel.frame = CGRectMake(self.nameLab.left, bottom, 275 * SCREENH6, labH);
    
    bottom = self.contentLabel.bottom + kPaddingValue;
    
    CGFloat top = bottom;
    if (model._child.count > 0) {
        
        self.listArray = [NSArray yy_modelArrayWithClass:[ChildModel class] json:model._child];
        
//        CGFloat height = 30 * SCREENH6 * model._child.count;
//        for (ChildModel * model in self.listArray) {
//
//            height += model.rowHeight;
//            NSLog(@"he===%f",height);
//        }

//        self.tableView.frame = CGRectMake(self.nameLab.left, bottom, 275 * SCREENH6, 95 * SCREENH6);
//
//         bottom = self.tableView.bottom + kPaddingValue;
        
        for (int i = 0; i < self.listArray.count; i++) {
            
            ChildModel * model = self.listArray[i];
            
            UILabel * nameLab = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.left + 10 * SCREENH6, top, 250 * SCREENH6, 12 * SCREENH6)];
            
            nameLab.textColor = ColorWithRGB(0xFAC917);
            nameLab.font = [UIFont systemFontOfSize:12 * SCREENH6];
            nameLab.text = [NSString stringWithFormat:@"%@:",model.user_name];
            nameLab.userInteractionEnabled = YES;
            nameLab.tag = i;
             UITapGestureRecognizer * tapNameGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNameAction:)];
            [nameLab addGestureRecognizer:tapNameGR];
            
            [self.nameIDArray addObject:model.user_id];
            [self.nameArray addObject:model.user_name];
            
            [self.backView addSubview:nameLab];
            
            top = nameLab.bottom + 5 * SCREENH6;
            
            UILabel * contentLabel = [UILabel new];
            
            contentLabel.textColor = ZRBlackColor;
            contentLabel.numberOfLines = 0;
            contentLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
            contentLabel.text = model.content;
            contentLabel.userInteractionEnabled = YES;
            contentLabel.tag = i;
            UITapGestureRecognizer * tapContentGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapContentAction:)];
            [contentLabel addGestureRecognizer:tapContentGR];
            
            [self.repleyIDArray addObject:model.id];
            
            [self.backView addSubview:contentLabel];
            
            CGSize attrStrSize = [self preferredSizeWithMaxWidth:250 * SCREENH6 withLabel:contentLabel];
            CGFloat labH = attrStrSize.height;
            
            contentLabel.frame = CGRectMake(nameLab.left, top, 250 * SCREENH6, labH);
            
            top = contentLabel.bottom + 7 * SCREENH6;
        }
        
        UIButton * moreButton = [[UIButton alloc] initWithFrame:CGRectMake(self.nameLab.left + 10 * SCREENH6, top, 80 * SCREENH6, 14 * SCREENH6)];
        
        top = moreButton.bottom + 7 * SCREENH6;
        
        [moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
        [moreButton setTitleColor:ColorWithRGB(0xFAC917) forState:UIControlStateNormal];
        [moreButton addTarget:self action:@selector(clickMoreButton) forControlEvents:UIControlEventTouchUpInside];
        moreButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
        moreButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        moreButton.
        [self.backView addSubview:moreButton];
        
        self.commiteBackView.frame = CGRectMake(self.nameLab.left, self.contentLabel.bottom + 5 * SCREENH6, 275 * SCREENH6, top - bottom);
        
        bottom = self.commiteBackView.bottom + 10 * SCREENH6;
    }


   
    
//    rowHeight = bottom + kBlank;
    
    self.backView.frame = CGRectMake(15 * SCREENH6, 0 * SCREENH6, 345 * SCREENH6, bottom);
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    
    _model.rowHeight = bottom;
}


- (void)setTopicModel:(TopicCommitModel *)topicModel{
    
    _topicModel = topicModel;
    
    self.ID = [NSString stringWithFormat:@"%@",topicModel.id];
    self.user_name = topicModel.user_name;
    
//    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,topicModel.head_img];
//    NSURL * url = [NSURL URLWithString:iconStr];
//    // 头像
//    [self.headImageView sd_setImageWithURL:url];
    if([topicModel.head_img containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:topicModel.head_img];
        [self.headImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,topicModel.head_img];
        NSURL * url = [NSURL URLWithString:iconStr];
        [self.headImageView sd_setImageWithURL:url];
    }
    // 昵称
    self.nameLab.text = topicModel.user_name;
    int gender = [topicModel.sex intValue];
    if (gender == 1) {
        
        self.genderImageView.image = [UIImage imageNamed:@"男性"];
    }else if (gender == 2){
        
        self.genderImageView.image = [UIImage imageNamed:@"女性"];
    }
    
    self.timeLabel.text = topicModel.inputtime;
    self.contentLabel.text = topicModel.content;
    
    CGFloat bottom = self.timeLabel.bottom + kPaddingValue;
    //    CGFloat rowHeight = 0;
    
    CGSize attrStrSize = [self preferredSizeWithMaxWidth:275 * SCREENH6 withLabel:self.contentLabel];
    CGFloat labH = attrStrSize.height;
    
    self.contentLabel.frame = CGRectMake(self.nameLab.left, bottom, 275 * SCREENH6, labH);
    
    bottom = self.contentLabel.bottom + kPaddingValue;
    
    CGFloat top = bottom;
    if (topicModel._child.count > 0) {
        
        self.listArray = [NSArray yy_modelArrayWithClass:[ChildModel class] json:topicModel._child];
        
        //        CGFloat height = 30 * SCREENH6 * model._child.count;
        //        for (ChildModel * model in self.listArray) {
        //
        //            height += model.rowHeight;
        //            NSLog(@"he===%f",height);
        //        }
        
        //        self.tableView.frame = CGRectMake(self.nameLab.left, bottom, 275 * SCREENH6, 95 * SCREENH6);
        //
        //         bottom = self.tableView.bottom + kPaddingValue;
        
        for (int i = 0; i < self.listArray.count; i++) {
            
            ChildModel * model = self.listArray[i];
            
            UILabel * nameLab = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.left + 10 * SCREENH6, top, 250 * SCREENH6, 12 * SCREENH6)];
            
            nameLab.textColor = ColorWithRGB(0xFAC917);
            nameLab.font = [UIFont systemFontOfSize:12 * SCREENH6];
            nameLab.text = [NSString stringWithFormat:@"%@:",model.user_name];
            nameLab.userInteractionEnabled = YES;
            nameLab.tag = i;
            UITapGestureRecognizer * tapNameGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNameAction:)];
            [nameLab addGestureRecognizer:tapNameGR];
            
            [self.nameIDArray addObject:model.user_id];
            [self.nameArray addObject:model.user_name];
            
            [self.backView addSubview:nameLab];
            
            top = nameLab.bottom + 5 * SCREENH6;
            
            UILabel * contentLabel = [UILabel new];
            
            contentLabel.textColor = ZRBlackColor;
            contentLabel.numberOfLines = 0;
            contentLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
            contentLabel.text = model.content;
            contentLabel.userInteractionEnabled = YES;
            contentLabel.tag = i;
            UITapGestureRecognizer * tapContentGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapContentAction:)];
            [contentLabel addGestureRecognizer:tapContentGR];
            
            [self.repleyIDArray addObject:model.id];
            
            [self.backView addSubview:contentLabel];
            
            CGSize attrStrSize = [self preferredSizeWithMaxWidth:250 * SCREENH6 withLabel:contentLabel];
            CGFloat labH = attrStrSize.height;
            
            contentLabel.frame = CGRectMake(nameLab.left, top, 250 * SCREENH6, labH);
            
            top = contentLabel.bottom + 7 * SCREENH6;
        }
        
        UIButton * moreButton = [[UIButton alloc] initWithFrame:CGRectMake(self.nameLab.left + 10 * SCREENH6, top, 80 * SCREENH6, 14 * SCREENH6)];
        
        top = moreButton.bottom + 7 * SCREENH6;
        
        [moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
        [moreButton setTitleColor:ColorWithRGB(0xFAC917) forState:UIControlStateNormal];
        [moreButton addTarget:self action:@selector(clickMoreButton) forControlEvents:UIControlEventTouchUpInside];
        moreButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
        moreButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        moreButton.
        [self.backView addSubview:moreButton];
        
        self.commiteBackView.frame = CGRectMake(self.nameLab.left, self.contentLabel.bottom + 5 * SCREENH6, 275 * SCREENH6, top - bottom);
        
        bottom = self.commiteBackView.bottom + 10 * SCREENH6;
    }
    
    
    
    
    //    rowHeight = bottom + kBlank;
    
    self.backView.frame = CGRectMake(15 * SCREENH6, 0 * SCREENH6, 345 * SCREENH6, bottom);
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    
    _topicModel.rowHeight = bottom;
}


- (void)tapAction{
    
//    NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
//    NSString * user_name = self.model.user_name;
    NSDictionary * dict = @{@"ID":self.ID,@"user_name":self.user_name};
//    NSString * ID = self.model._child.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"commitSomePeoPle" object:nil userInfo:dict];
}


//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//
//    return 1;
//}
//
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//
//    //    NSLog(@"aaa===%lu",(unsigned long)self.localModelArray.count);
//    return self.listArray.count;
//}
//
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    SecondaryCommitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondaryCommitTableViewCell" forIndexPath:indexPath];
//
//    cell.model = self.listArray[indexPath.row];
//
//    return cell;
//}
//
//#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    ChildModel * model = self.listArray[indexPath.row];
//
////    self.tableView.height += model.rowHeight;
////    self.backView.height += model.rowHeight;
////    self.model.rowHeight += model.rowHeight;
//    return model.rowHeight;
//}


- (CGSize)preferredSizeWithMaxWidth:(CGFloat)maxWidth withLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, 10000000.0f)];
    size.width = fmin(size.width, maxWidth); //在numberOfLine为1模式下返回的可能会比maxWidth大，所以这里我们限制下
    return size;
}


- (void)tapNameAction:(UITapGestureRecognizer *)sender{
    
    UILabel * label = (UILabel *)sender.view;
    
    NSString * user_id = self.nameIDArray[label.tag];
    
    NSDictionary * dict = @{@"user_id":user_id};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToPerson" object:nil userInfo:dict];
}

- (void)tapContentAction:(UITapGestureRecognizer *)sender{
    
    UILabel * label = (UILabel *)sender.view;
    
    NSString * ID = self.repleyIDArray[label.tag];
    NSString * user_name = self.nameArray[label.tag];
    NSDictionary * dict = @{@"ID":ID,@"user_name":user_name};
    //    NSString * ID = self.model._child.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"commitSomePeoPle" object:nil userInfo:dict];
}


- (void)clickMoreButton{
    
   NSDictionary * dict = @{@"ID":self.ID};
    
   [[NSNotificationCenter defaultCenter] postNotificationName:@"PushToMoreCommit" object:nil userInfo:dict];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
